package com.elong.businesstravel.modules.home.adapter;

import java.util.List;

import com.elong.businesstravel.R;
import com.elong.businesstravel.entitys.Hotel;
import com.elong.businesstravel.entitys.HotelDocs;
import com.elong.businesstravel.entitys.HotelSearchCondition;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
/**
 * 酒店列表
 * @author lenovo
 *
 */
public class HotelSearchResultAdapter extends BaseAdapter {

	protected Context mContext;
	protected List<HotelDocs> mHotelList;
	protected HotelSearchCondition mCurrentHotelSearchCondition;

	public HotelSearchResultAdapter(Context context, List<HotelDocs> hotelList,
			HotelSearchCondition currentCondition) {
		mContext = context;
		mHotelList = hotelList;
		mCurrentHotelSearchCondition = currentCondition;
	}

	@Override
	public int getCount() {
		return mHotelList.size();
	}

	@Override
	public Object getItem(int position) {
		return mHotelList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_hotel_search_result, null);
		}

		HotelDocs hotelDocs = mHotelList.get(position);

		TextView hotelNametextView = (TextView) convertView
				.findViewById(R.id.hotelNametextView);
		hotelNametextView.setText(hotelDocs.hotelName);

		RatingBar realRatingBar = (RatingBar) convertView
				.findViewById(R.id.realRatingBar);
		RatingBar hotelRatingBar = (RatingBar) convertView
				.findViewById(R.id.hotelRatingBar);
		realRatingBar.setVisibility(View.GONE);
		hotelRatingBar.setVisibility(View.GONE);

		if (TextUtils.isEmpty(hotelDocs.realStar)) {

			realRatingBar.setVisibility(View.GONE);
			hotelRatingBar.setVisibility(View.GONE);

		} else if ("0".equals(hotelDocs.realStar)) {
			// 空心
			realRatingBar.setVisibility(View.VISIBLE);
			hotelRatingBar.setVisibility(View.GONE);

			if (TextUtils.isEmpty(hotelDocs.hotelStar)
					|| "0".equals(hotelDocs.hotelStar)) {
				realRatingBar.setVisibility(View.GONE);
			} else {
				realRatingBar
						.setProgress(Integer.parseInt(hotelDocs.hotelStar));
			}

		} else if ("1".equals(hotelDocs.realStar)) {

			// 实心
			realRatingBar.setVisibility(View.GONE);
			hotelRatingBar.setVisibility(View.VISIBLE);

			if (TextUtils.isEmpty(hotelDocs.hotelStar)
					|| "0".equals(hotelDocs.hotelStar)) {
				hotelRatingBar.setVisibility(View.GONE);

			} else {
				hotelRatingBar.setProgress(Integer
						.parseInt(hotelDocs.hotelStar));
			}
		}

		ImageView smallDiKouImageView = (ImageView)convertView.findViewById(R.id.smallDiKouImageView);
		smallDiKouImageView.setVisibility(View.GONE);
		if("1".equals(hotelDocs.deduction)){
			smallDiKouImageView.setVisibility(View.VISIBLE);
		}
		ImageView parkingImageView = (ImageView) convertView
				.findViewById(R.id.parkingImageView);
		parkingImageView.setVisibility(View.GONE);
		ImageView wifiImageView = (ImageView) convertView
				.findViewById(R.id.wifiImageView);
		wifiImageView.setVisibility(View.GONE);
		if (!"".equals(hotelDocs.facilities)) {
			String[] facilities = hotelDocs.facilities.split(",");
			for (int i = 0; i < facilities.length; i++) {

				if ("1".equals(facilities[i])) {
					// wifi
					wifiImageView.setVisibility(View.VISIBLE);
				} else if ("5".equals(facilities[i])) {
					// parking
					parkingImageView.setVisibility(View.VISIBLE);
				}
			}
		}

		TextView hotelMoneyTextView = (TextView) convertView
				.findViewById(R.id.hotelMoneyTextView);
		if ("0".equals(hotelDocs.minPrice)) {
			hotelMoneyTextView.setVisibility(View.GONE);
		} else {
			hotelMoneyTextView.setVisibility(View.VISIBLE);
			
			SpannableStringBuilder moneySpannable = new SpannableStringBuilder(
					"￥ " + hotelDocs.minPrice);
			moneySpannable.setSpan(
					new ForegroundColorSpan(mContext.getResources()
							.getColor(R.color.list_gray_text_color)),
					0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			float moneySize = mContext.getResources().getDimension(
					R.dimen.list_text_small_size);
			moneySpannable.setSpan(
					new AbsoluteSizeSpan((int) moneySize), 0, 1,
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			
			hotelMoneyTextView.setText(moneySpannable);
		}

		ViewGroup fanLayout = (ViewGroup) convertView
				.findViewById(R.id.fanLayout);
		ImageView fanImageView = (ImageView) convertView
				.findViewById(R.id.fanImageView);
		TextView fanMoneyextView = (TextView) convertView
				.findViewById(R.id.fanMoneyextView);
		if (TextUtils.isEmpty(hotelDocs.bonus) || "0".equals(hotelDocs.bonus)) {
			fanLayout.setVisibility(View.GONE);
		} else {
			fanLayout.setVisibility(View.VISIBLE);
			fanMoneyextView.setText(hotelDocs.bonus);
		}
		
		ViewGroup manfangLayout = (ViewGroup)convertView.findViewById(R.id.manfangLayout);
		ViewGroup moneyLayout = (ViewGroup)convertView.findViewById(R.id.moneyLayout);
		manfangLayout.setVisibility(View.GONE);
		moneyLayout.setVisibility(View.GONE);
		if("1".equals(hotelDocs.roomstatus)){
			//满房 
			manfangLayout.setVisibility(View.VISIBLE);
			moneyLayout.setVisibility(View.GONE);
		}else {
			
			//不满房
			manfangLayout.setVisibility(View.GONE);
			moneyLayout.setVisibility(View.VISIBLE);
		}
		

		TextView landmarksTextView = (TextView) convertView
				.findViewById(R.id.landmarksTextView);

		if (!"".equals(hotelDocs.distance)) {
			if (!TextUtils.isEmpty(mCurrentHotelSearchCondition.piloting)) {
				if (mCurrentHotelSearchCondition.piloting
						.equals(hotelDocs.hotelName)
						|| mCurrentHotelSearchCondition.piloting
								.equals(hotelDocs.hotelName.replace("酒店", ""))) {
					landmarksTextView.setText(hotelDocs.districtName + " "
							+ hotelDocs.bussName);
				} else {
					landmarksTextView.setText("距"
							+ mCurrentHotelSearchCondition.piloting + " "
							+ hotelDocs.distance);
				}
			} else {
				if ("0米".equals(hotelDocs.distance)) {
					landmarksTextView.setText(hotelDocs.districtName + " "
							+ hotelDocs.bussName);
				} else {
					String nameString = mCurrentHotelSearchCondition.mSearchConditionPoi.name;
					if ("".equals(nameString)) {
						nameString = mCurrentHotelSearchCondition.mSearchConditionPoi.bussinename;
					}
					if ("".equals(nameString)) {
						nameString = mCurrentHotelSearchCondition.mSearchConditionPoi.districtsname;
					}

					if ("当前位置".equals(nameString)) {
						landmarksTextView.setText("距 您" + hotelDocs.distance);
					} else {
						landmarksTextView.setText("距 " + nameString + " "
								+ hotelDocs.distance);
					}
				}
			}
		} else {
			landmarksTextView.setText(hotelDocs.districtName + " "
					+ hotelDocs.bussName);
		}

		return convertView;
	}
}
